package cn.jekun.shop.app.action;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.action.ComponentCoreAction;
import cn.jekun.component.core.bean.gen.RsUser;
import cn.jekun.shop.app.service.LoginService;
import cn.jekun.shop.web.bean.MemberSession;
import cn.jekun.shop.web.bean.User;

@SuppressWarnings("serial")
public class AppBaseAction extends ComponentCoreAction{
	@Resource(name = LoginService.BEAN_NAME)
	LoginService loginService;
	
	/** 
	 * 返回当前登录的用户信息
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected MemberSession getSessionUser() {
		//TODO 商户情况？？？
		String tempUserId = this.getUserId();
		Map tempUserMap = new HashMap();
		tempUserMap.put(RsUser.COL_USER_ID, tempUserId);
		SqlUtis.prepareSql(tempUserMap, "", SqlUtis.getSQL(tempUserMap, "b."
				+ RsUser.COL_USER_ID, Types.VARCHAR, SqlUtis.EQ));
		User user = this.loginService.one(tempUserMap);
		MemberSession session = new MemberSession();
		session.setUserId(user.getUserId());
		session.setShopId(user.getShopId());
		session.setUserName(user.getUserName());
		session.setMobile(user.getMobile());
		return session;
	}
	
	/**
	 * 接口获取不到浏览器的，看传递过来的手机型号
	 */
	@Override
	public String getBrowser() {
		String terminalId = request.getParameter("terminalId");//手机的唯一标识
		String model = request.getParameter("model");//机器的型号+“_”+系统版本
		if (StringUtils.isBlank(terminalId) && StringUtils.isBlank(model)) {
			//return super.getBrowser();
			return "wap";
		}
		return (model==null?"":model)+(terminalId==null?"":"("+terminalId+")" );
	}
}
